﻿using System;

namespace EasyIot.WinApp
{
    /// <summary>
    /// 协议处理类
    /// </summary>
    public class LockHandle
    {
        public bool Process(int cmd, ByteBuffer bBuffer)
        {
            // 协议解析
            int iPosition = bBuffer.GetPosition(); if (!bBuffer.HasRemaining()) return false;
            byte head1 = 0; byte head2 = 0; byte head3 = 0; byte head4 = 0; bool headok = false;

            while (bBuffer.HasRemaining())
            {
                head1 = bBuffer.GetByte(); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }
                if (LockSerialPort.HEAD1 == head1)
                {
                    iPosition = bBuffer.GetPosition() - 1;
                    head2 = bBuffer.GetByte(); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }
                    head3 = bBuffer.GetByte(); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }
                    head4 = bBuffer.GetByte(); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }
                    if (LockSerialPort.HEAD2 == head2 && LockSerialPort.HEAD3 == head3 && LockSerialPort.HEAD4 == head4)
                    {
                        headok = true;
                        break;
                    }
                    else
                    {
                        LogHelper.AppendLog("Error,Unable to parse the data(2) Lock:Position=" + iPosition.ToString() + ",Index=" + bBuffer.Position.ToString() + ",Data=" + head2.ToString());
                    }
                }
                else
                {
                    LogHelper.AppendLog("Error,Unable to parse the data(1) Lock:Position=" + iPosition.ToString() + ",Index=" + bBuffer.Position.ToString() + ",Data=" + head2.ToString());
                }
            }

            if (!bBuffer.HasRemaining())
            {
                if (headok) bBuffer.Position = iPosition;// 头正确继续等待接收
                return false;
            }

            byte bagLen = bBuffer.GetByte(); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }
            if (bagLen < 8)
            {
                LogHelper.AppendLog("Error,bagLen<8");
                bBuffer.Clear();
                return false;
            }
            byte board = bBuffer.GetByte(); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }
            byte cmdRecv = bBuffer.GetByte(); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }
            int dataLen = bagLen - 8;
            byte[] request = bBuffer.GetByteArray(dataLen); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }
            byte check = bBuffer.GetByte(); if (bBuffer.WaitRecvRemain) { bBuffer.Position = iPosition; return false; }

            if (!bBuffer.HasRemaining())
            {
                bBuffer.Clear();
            }

            if (!LockSerialPort.CheckXOR(bagLen, board, cmdRecv, request, check))
            {
                LogHelper.AppendLog("Error,CheckXOR");
            }
            else
            {
                try
                {
                    switch (cmd)
                    {
                        case (byte)0x82:
                            // 开单个通道锁
                            if (3 == request.Length)
                            {
                                byte status1 = request[0];
                                byte channel = request[1];
                                byte lockstatus = request[2];
                                string note = lockstatus == 0 ? "成功" : "失败"; 
                                DataMgr.MainHandle.OnMessage("硬件反馈,板号:" + board + ",通道号:" + channel + ",状态:" + note);
                            }
                            break; 
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.AppendLog("Error,GK,errmsg:" + ex.Message);
                }
            }

            return cmd == cmdRecv ? true : false;
        }
    }
}